package com.app.lol.jungleTime;

import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import de.ksquared.system.keyboard.GlobalKeyListener;
import de.ksquared.system.keyboard.KeyAdapter;
import de.ksquared.system.keyboard.KeyEvent;

import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Rectangle;

public class MasterView {

	//------------------------
	//		Attributes
	//------------------------
	
	/** Master Model */
	private MasterModel masterModel;
	
	/** Master Controller */
	private MasterController masterController;
	
	/** Main frame */
	JFrame window = new JFrame("Respawn time");
	
	/** One chrono for each monster */
	List<TextChronoField> listTextChronoField = new ArrayList<TextChronoField>();
	
	/** One key for each chrono */
	List<Integer> actionKeyInt = new ArrayList<Integer>();
	List<String> actionKeyString = new ArrayList<String>();
	
	//------------------------
	//		Constructor
	//------------------------
	
	public MasterView(MasterModel masterModel) {
		
		this.masterModel = masterModel;
		
		initActionKeys();

		openWindow();
	}

	//------------------------
	//	    Main Method
	//------------------------
	
	public void openWindow()
	{
		
		@SuppressWarnings("unused")
		JDialog  myModalWindow = new JDialog(window); //Display window alway on top
		window.setAlwaysOnTop(true);
		window.setFocusable(false);
		
		window.setResizable(false);
		window.setUndecorated(true);
		window.setSize(760, 20);
		setWindowLocation();
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//Layout
		JPanel panel = new JPanel();
		GridLayout gridLayout = new GridLayout(1, 6);
		panel.setLayout(gridLayout);
		window.setContentPane(panel);
		
		//Start listener on keyboard
		setListenerOnKeyboard();
		
		
		//Labels and text fields
		Font commonFont = new Font("Dialog", Font.BOLD, 16);
		for (int i = 0; i < masterModel.getListOfMonsterChrono().size(); i++) {
			
			MonsterChrono monsterChrono = masterModel.getListOfMonsterChrono().get(i);

			//Label
			JLabel label = new JLabel(monsterChrono.getChronoName() + " : ");
			label.setHorizontalAlignment(JLabel.CENTER);
			label.setFont(commonFont);
			label.setForeground(monsterChrono.getColor());
			panel.add(label);

			//Text field (one action key by chrono)
			TextChronoField textChronoField = new TextChronoField();
			textChronoField.setName(monsterChrono.getChronoName());
			textChronoField.setText("Push " + actionKeyString.get(i).toUpperCase());
			textChronoField.setEditable(false);
			textChronoField.setBorder(BorderFactory.createLineBorder(window.getBackground()));
			textChronoField.setHorizontalAlignment(JTextField.CENTER);

			panel.add(textChronoField);

			listTextChronoField.add(textChronoField);
		}

		window.setVisible(true);
		
		Thread a = new Thread(
				new Runnable() {
					public void run() {
						while(true){
							window.setAlwaysOnTop(true);
						}
					}
				});
		a.start();
	}
	
	/**
	 * Init action key which are enable
	 */
	private void initActionKeys() {
		actionKeyInt.add(100);
		actionKeyString.add("4");
		actionKeyInt.add(101);
		actionKeyString.add("5");
		actionKeyInt.add(102);
		actionKeyString.add("6");
		actionKeyInt.add(97);
		actionKeyString.add("1");
		actionKeyInt.add(98);
		actionKeyString.add("2");
		actionKeyInt.add(99);
		actionKeyString.add("3");
	}
	
	
	/**
	 * Place the window on top
	 */
	private void setWindowLocation() {
		Rectangle bounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
		Point topLeft = new Point((bounds.width/2) - (window.getWidth()/2), 0);
		window.setLocation(topLeft);
	}
	
	/**
	 * This listener is enable evrywhere, even if you loose window focus
	 * @see http://ksquared.de/blog/2011/07/java-global-system-hook/
	 */
	public void setListenerOnKeyboard() {

		new GlobalKeyListener().addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent event) {
				
				int indexOfMonster = actionKeyInt.indexOf(event.getVirtualKeyCode());
				
				//System.out.println(event.getVirtualKeyCode());
				
				if(indexOfMonster != -1)
				{
					masterController.startChrono(indexOfMonster);
				}
			}
			@Override
			public void keyReleased(KeyEvent event) {}
		});
	}

	//------------------------
	//	 Setter/Getter/Is
	//------------------------
	
	public void setMasterController(MasterController masterController) {
		this.masterController = masterController;
	}
	
	public List<TextChronoField> getListMonsterChrono() {
		return listTextChronoField;
	}

}